import apiClient from './client'
import type { User, CreateUserRequest, PageRequest, PageResponse } from '../types'

export const userService = {
  // 创建用户
  createUser: async (data: CreateUserRequest): Promise<User> => {
    const response = await apiClient.post<User>('/users', data)
    return response.data
  },

  // 获取用户信息（通过ID）
  getUserById: async (id: number): Promise<User> => {
    const response = await apiClient.get<User>(`/users/${id}`)
    return response.data
  },

  // 获取用户信息（通过用户名）
  getUserByUsername: async (username: string): Promise<User> => {
    const response = await apiClient.get<User>(`/users/username/${username}`)
    return response.data
  },

  // 获取所有用户（分页）
  getAllUsers: async (params?: PageRequest): Promise<PageResponse<User>> => {
    const response = await apiClient.get<PageResponse<User>>('/users', { params })
    return response.data
  },

  // 更新用户信息
  updateUser: async (id: number, data: Partial<CreateUserRequest>): Promise<User> => {
    const response = await apiClient.put<User>(`/users/${id}`, data)
    return response.data
  },

  // 删除用户
  deleteUser: async (id: number): Promise<void> => {
    await apiClient.delete(`/users/${id}`)
  },
}
